package com.example.baseservice.controller;

import com.example.baseservice.bean.Goods;
import com.example.baseservice.common.DataGridView;
import com.example.baseservice.common.LayuiJson;
import com.example.baseservice.common.ResultObj;
import com.example.baseservice.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    LayuiJson layuiJson;

    /**
     * 根据条件动态查询商品
     *
     * @param
     * @return
     */
    @RequestMapping("loadAllGoods")
    public LayuiJson<Goods> loadAllGoods(Integer providerid, String goodsname, String productcode, String promitcode, String description
            , String size, Integer page, Integer limit) {
        page = page == null ? 1 : page;
        limit = limit == null ? 5 : limit;
        int index = (page - 1) * limit;
        List<Goods> data = goodsService.select(providerid, goodsname, productcode, promitcode, description, size, index, limit);
        layuiJson.setCode(0);
        layuiJson.setCount(1000);
        layuiJson.setData(data);
        return layuiJson;
    }

    /**
     * 可选商品（下拉框）
     *
     * @param
     * @return
     */
    @RequestMapping("loadAllGoodsForSelect")
    public DataGridView loadAllGoodsForSelect() {
        List<Goods> list = goodsService.selectGoodsName();
        return new DataGridView(list);
    }

    /**
     * 查询对应供应商提供的商品
     *
     * @param
     * @return
     */
    @RequestMapping("loadGoodsByProviderId")
    public DataGridView loadGoodsByProviderId(Integer providerid) {
        List<Goods> list = goodsService.selectByProviderid(providerid);
        return new DataGridView(list);
    }


    /**
     * 添加商品
     *
     * @param
     * @return
     */
    @RequestMapping("addGoods")
    public ResultObj addGoods(Integer providerid, String goodsname, String productcode, String promitcode, String description,
                              String size, String produceplace, String goodspackage, Double price, Integer number, Integer dangernum,
                              Integer available) {
        try {
            goodsService.insert(providerid, goodsname, productcode, promitcode, description, size, produceplace, goodspackage, price, number, dangernum, available);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改商品
     *
     * @param
     * @return
     */
    @RequestMapping("updateGoods")
    public ResultObj updateGoods(Integer id, Integer providerid, String goodsname, String productcode, String promitcode,
                                 String description, String size, String produceplace, String goodspackage, Double price, Integer number,
                                 Integer dangernum, Integer available) {
        try {
            goodsService.update(id, providerid, goodsname, productcode, promitcode, description, size, produceplace, goodspackage, price, number, dangernum, available);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除商品
     *
     * @param id 商品id
     * @return
     */
    @RequestMapping("deleteGoods")
    public ResultObj deleteGoods(Integer id) {
        try {
            goodsService.delete(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
